package cn.tedu.course.employee.service;

import cn.tedu.course.employee.mapper.EmployeeMapper;
import cn.tedu.course.employee.pojo.dto.EmployeeParam;
import cn.tedu.course.employee.pojo.dto.EmployeeQuery;
import cn.tedu.course.employee.pojo.entity.Employee;
import cn.tedu.course.employee.pojo.vo.EmployeeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: course-jsd2503-student
 * @BelongsPackage: cn.tedu.course.employee.service
 * @Author: yinwenkang
 * @CreateTime: 2025-06-14  11:41
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService{
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public List<EmployeeVO> selectEmp(EmployeeQuery employeeQuery) {
        log.debug("查询用户业务： employeeQuery={}", employeeQuery);
        List<EmployeeVO> list = employeeMapper.select(employeeQuery);
        return list;
    }

    @Override
    public void updateStatus(Long id,String status) {
        log.debug("更新用户状态业务： id={} status={}", id, status);
        String newStatus;
        if ("0".equals(status)) {
            newStatus = "1";
        } else if ("1".equals(status)) {
            newStatus = "0";
        } else {
            // 处理非法状态值（可选）
            log.warn("无效的状态值: {}", status);
            newStatus = status; // 保持原值或根据需求处理
        }
        employeeMapper.updateStatus(id,newStatus);
    }

    @Override
    public void saveEmp(EmployeeParam employeeParam) {
        log.debug("保存用户业务： employeeParam={}", employeeParam);
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeParam,employee);
        if (employee.getId() == null){
            //新增
            employee.setCreateTime(new Date());
            employeeMapper.insert(employee);
        } else {
            //更新
            employee.setUpdateTime(new Date());
            employeeMapper.update(employee);
        }
    }
}
